#include <gtest/gtest.h>
#include "util/stdc.h"
#include "util/proper.h"
#include "util/logger.h"
#include "util/string.h"
#include "util/pinger.h"
#include "util/time.h"

int main_entry()
{
	util::Proper proper;
	if (proper.load("pingit.conf"))
	{
		printf("fail to load pingit.conf\n");
		return 1;
	}

	string server_str = proper.get("server.list", "");
	std::vector<string> server_vec = util::split(server_str, ';');

	if (server_str.empty() || server_vec.empty())
	{
		printf("fail to get server.list\n");
		return 1;
	}

	util::Logger logger;
	if (logger.open("pingit.log", 7, true))
	{
		printf("fail to open log file\n");
		return 1;
	}

	util::Pinger pinger;

	while (true)
	{
		size_t num = server_vec.size();
		for (size_t i=0; i<num; ++i)
		{
			int ttl, rtt;
			int ret = pinger.ping(server_vec[i].c_str(), ttl, rtt);

			if (ret)
			{
				logger.log_info("|", "from %-15s [%s]    timeout (%d)",
					server_vec[i].c_str(), proper.get(server_vec[i], "").c_str(), ret);
			}
			else
			{
				logger.log_info("|", "from %-15s [%s]   rtt:%dms    TTL=%d",
					server_vec[i].c_str(), proper.get(server_vec[i], "").c_str(), rtt, ttl);
			}
		}

		util::sleep(10000);
	}

	return 0;
}

int main(int argc, char *argv[])
{
	main_entry();
	printf("press any key to exit!\n");
	getchar();
	return 0;
}
